﻿var mall_obj = {



	//产品列表页
	index_info: function(){
		
		loadpage=1;
		mall_obj.load_product();
		
		var top=0;	
		$(window).scroll(function(){
			oldtop = top; 
			top=$(window).scrollTop();
			var height=$(document).innerHeight()
			var liheight=$(window).innerHeight()
			if(top>oldtop){
				var bottom=height-liheight-top;
				console.log(bottom);
				if(bottom==0&&load==true){
					//loadpage++;
					mall_obj.load_product();
				}
			}
		});

	},
	//载入团购列表
	load_product:function(){
		$(".loading_box").show();
		load=false;
		$.post('?s=Mall/index/p/' + loadpage,  function(data){
 
        	if(data.ret==1){
        		var html="";
        			for(var i=0;i<data.msg.length;i++){

        				html+='<div class="indpart">'
        				html+='<div class="banner">'
        				html+='<div class="jtpic"><img src="'+data.msg[i]['coverimg']+'"></div>'
        				html+='<div class="words"><img src="./public/Wechat/api/images/bannerxfbg.png"><i>'+data.msg[i]['remark']+'</i></div>'
        				html+='</div>'
        				html+='<div class="productinfor">'
        				html+='<h2>'+data.msg[i]['name']+'</h2>'
        				html+='<p>市场价：<i>￥'+data.msg[i]['oriprice']+'</i></p>'
        				html+='<div><i>成团价：<em>￥'+data.msg[i]['lowestprice']+'</em>起</i><a href="?s=Mall/productdetail/id/'+data.msg[i]['id']+'">去开团<img src="./public/Wechat/api/images/arrowr.png"></a></div>'
        				html+='<span>当前已有<ins>'+data.msg[i]['buynumber']+'</ins>个团队开团啦！</span>'
        				html+='</div>'
        				html+='</div>'
        			}
        			$(".wrap").append(html);
        			$(".loading_box").hide();
        			//$(".coupon_container").show();
        			//$(".get_coupon_button").bind("click",vip_obj.getcoupon_init);
        			loadpage++;	
        			load=true;
        	}else{
        		$(".loading_box").hide();
        	}
        	
        }, 'json');
		
	},
	productDetail:function(){
		$(".selectprice .plus").click(function() {
	        var a = $(this).parent().find(".webtxt");
	        a.val(parseInt(a.val()) + 1),parseInt(a.val()) >= $(".restnum").html()&& a.val($(".restnum").html()) ;
	    })
		$(".selectprice .minus").click(function() {
	        var a = $(this).parent().find(".webtxt");
	        a.val(parseInt(a.val()) - 1), parseInt(a.val()) <= 1 && a.val(1);
	    })
		$(".goodsprices ul li a").click(function(){
			if($(this).attr("status")=="5"){alert("该团已取消！"); return false;}
			if($(this).attr("status")=="6"){alert("该团已过期！"); return false;}
			if($(this).attr("num")=="jion"){alert("您已经参加该团！"); return false;}
			if($(this).attr("num")=="full"){alert("参团人数已满！"); return false;}
			html=$(".goodsprices ul li a").eq(0).html();
			var nid=$(this).attr("nid");
			$("#productgroupid").val(nid);
			$.post('?s=Mall/productDetail','nid='+nid,  function(data){
				if(data.ret==1){
					$(".price").html(data.msg.price);
				}
			});
			$(".graybg").fadeIn();
			$(".selectprice").slideDown();
			$(this).html("立即购买");
		})
		$(".selectprice .closed").click(function() {
	        $(".graybg").fadeOut();
			$(".selectprice").slideUp();
			//var html='去开团<img src="./public/Wechat/api/images/arrowr.png">'
			$(".goodsprices ul li a").html(html);
	    })
		$(".selectprice .part2 li i").click(function(){
			$(".selectprice .part2 li i").removeClass("active");
			$(this).addClass("active");
			var restnumber=$(this).attr("restnumber");
			$(".restnum").html(restnumber);
		 })
		 $(".selectprice .part2 li i").eq(0).addClass("active");
		 $(".selectprice .part2 li i").each(function(){
			 if($(this).hasClass("active")){
				
				 var restnumber=$(this).attr("restnumber");
				 $(".restnum").html(restnumber);
			 }
		 })
		 
		
	    $(".btns").click(function(){
	    	if($(".restnum").html()<=0){alert("库存不足！"); return false;}
	    	var sid=$(".selectprice .part2 li .active").attr("sid");
	    	var productgroupid=$("#productgroupid").val();
	    	var num=$(".webtxt").val();
	    	//var productid=$(".productid").val();
	    	//window.location.href='?s=Mall/order.html';
	    	$.post('?s=Mall/goOrder','sid='+sid+'&productgroupid='+productgroupid+'&num='+num,  function(data){
	    		if(data.ret==1){
	    			window.location.href='?s=Mall/order.html';
	    		}
	    	});
	    })
	    
	    
	},
	address:function(){
		 $(".selectaddr .cont b").click(function(){
				$(".selectaddr .cont b").removeClass("active");
				$(this).addClass("active");
				if($(this).hasClass("active")){
					var addressid=$(this).attr("addressid");
					$.post('?s=Mall/goorder','addressid='+addressid,  function(data){
			    		if(data.ret==1){
			    			window.location.href='?s=Mall/order.html';
			    		}
			    	});
				}
			 })
	},
	address_edit:function(){
		$(".changeaddress li label").click(function() {
		       $(this).toggleClass("active");
		 });
		//判断是否为默认地址
		var isdefault="";
		$("label").click(function(){
			var sign=$(this).attr("class");
			if(sign=="active"){
				isdefault=1;
			}else{
				isdefault=0;
			}
		});
		$(".addrbtn").click(function(){
			if(global_obj.check_form($('*[notnull]'))){global_obj.win_alert("请完成信息！");return false}
			$.post("?s=/Mall/addressedit",$("#address_form").serialize()+"&isdefault="+isdefault,function(data){
				if(data.ret==1){
					alert(data.msg);
					window.location.href="?s=/Mall/address";
				}else{
					alert(data.msg);
				}
			});
		});
	},
	order:function(){
		click=true;
		$(".fixedwrap div a").click(function(){
			if($(".myaddress a span").hasClass("noaddress")){global_obj.win_alert("请选择地址！");return false}			
			if(click){
				var remark=$("#remark").val();
				click=false;
				$.post("?s=/Mall/doorder","remark="+remark,function(data){
					if(data.ret==1){
						window.location.href="?s=Mall/orderdetail/id/"+data.id
						//window.location.href="/wechat.php/vip/wxpay/";
					}
				});
			}else{
				alert("请不要重复提交！");
			}
			
		});
	},
	order_detail:function(){
		$(".btn").click(function(){
			var orderid=$(this).attr("id");
			$.post("?s=/Mall/orderpay","id="+orderid,function(data){
				if(data.ret==1){
					//window.location.href="?s=Mall/orderdetail/id/"+data.id
					window.location.href="/wechat.php/Mall/wxpay/";
				}
			});
		});
	},
}


